home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / SceneTexture.cpp < prev    next >
C/C++ Source or Header  |  2005-09-26  |  3KB  |  136 lines

  1.  
  2. #include "SceneTexture.h"
  3.  
  4. namespace peon
  5. {
  6.     SceneTexture::SceneTexture()
  7.     {
  8.     }
  9.  
  10.     SceneTexture::~SceneTexture()
  11.     {
  12.         glDeleteTextures(1, &m_tex );
  13.     }
  14.  
  15.     bool SceneTexture::loadImage( const String& strFilename, bool bAlpha,
  16.         bool bMipMaps, bool bRepeat )
  17.     {
  18.  
  19.         //load the image data to an SDL_Surface structure
  20.         SDL_Surface* pTexSurface = IMG_Load( strFilename.c_str() );
  21.         if( NULL == pTexSurface )
  22.         { 
  23.             //error
  24.             return false;
  25.         }
  26.  
  27.         //calculate the total size of the image data. If you are needing
  28.         //the alpha channel then account for that
  29.         int dim = pTexSurface->w * pTexSurface->h * ((bAlpha) ? 4: 3);
  30.         GLubyte *pData = new GLubyte[ dim ];
  31.  
  32.         //loop through our SDL_Surface and copy it into the array
  33.         //if the image has an extra alpha channel of information then
  34.         //be sure to append that
  35.         int pos = 0;
  36.         for( int y = (pTexSurface->h) - 1; y > -1; y-- )
  37.         {
  38.             for(int x = 0; x < pTexSurface->w; x++)
  39.             {
  40.                 Uint8 r, g, b, a;
  41.  
  42.                 //getPixel is defined in the SDL documentation. It just
  43.                 //grabs the pixel data from a given SDL_Surface at
  44.                 //coordinates x,y
  45.                 Uint32 color = getPixel(pTexSurface, x, y);
  46.  
  47.                 if(!bAlpha)
  48.                     SDL_GetRGB( color, pTexSurface->format, &r, &g, &b);
  49.  
  50.                 else
  51.                     SDL_GetRGBA( color, pTexSurface->format, &r, &g, &b, &a);
  52.  
  53.                 pData[pos] = r; pos++;
  54.                 pData[pos] = g; pos++;
  55.                 pData[pos] = b; pos++;
  56.                 if( bAlpha )
  57.                     pData[pos] = a; pos++;
  58.             }
  59.         }
  60.     
  61.         
  62.         int type = (bAlpha) ? GL_RGBA : GL_RGB;
  63.         glGenTextures(1, &m_tex);        // Generate texture ID
  64.         glBindTexture(GL_TEXTURE_2D, m_tex);
  65.  
  66.         int filter_min, filter_mag;
  67.  
  68.  
  69.         filter_min = (bMipMaps) ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST;
  70.         filter_mag = GL_NEAREST;
  71.  
  72.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  73.             filter_min);
  74.  
  75.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  76.             filter_mag);
  77.  
  78.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 
  79.             (bRepeat) ? GL_REPEAT : GL_CLAMP);
  80.  
  81.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 
  82.             (bRepeat) ? GL_REPEAT : GL_CLAMP);
  83.  
  84.         if(bMipMaps)
  85.         {
  86.             gluBuild2DMipmaps(GL_TEXTURE_2D, type, pTexSurface->w, pTexSurface->h,
  87.                 type, GL_UNSIGNED_BYTE, pData);
  88.         }else
  89.         {
  90.             glTexImage2D(GL_TEXTURE_2D, 0, type, pTexSurface->w,
  91.                 pTexSurface->h, 0, type, GL_UNSIGNED_BYTE, pData);
  92.         }
  93.  
  94.         //now that we are finished, do some garbage collection
  95.         //clean up our array and destroy the surface you loaded 
  96.         delete [] pData;
  97.         SDL_FreeSurface( pTexSurface );
  98.  
  99.         //return the texture handle
  100.         return true;
  101.     }
  102.  
  103.     /*
  104.     * Return the pixel value at (x, y)
  105.     * NOTE: The surface must be locked before calling this!
  106.     * Taken from SDL documentation.
  107.     */
  108.     Uint32 SceneTexture::getPixel(SDL_Surface *surface, int x, int y)
  109.     {
  110.         int bpp = surface->format->BytesPerPixel;
  111.         /* Here p is the address to the pixel we want to retrieve */
  112.         Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  113.  
  114.         switch(bpp) {
  115.         case 1:
  116.             return *p;
  117.  
  118.         case 2:
  119.             return *(Uint16 *)p;
  120.  
  121.         case 3:
  122.             if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  123.                 return p[0] << 16 | p[1] << 8 | p[2];
  124.             else
  125.                 return p[0] | p[1] << 8 | p[2] << 16;
  126.  
  127.         case 4:
  128.             return *(Uint32 *)p;
  129.  
  130.         default:
  131.             return 0;       /* shouldn't happen, but avoids warnings */
  132.         }
  133.     }
  134.  
  135. }
  136.